<?php

namespace App\Http\Controllers\Api\v2;

use App\Http\Controllers\ApiController;
use App\Models\Item;
use App\Models\ItemCatlog;

class ItemController extends ApiController
{

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function getItem(){
        $itemid = $this->request->input('itemid');
        $item = Item::with(['content', 'images', 'shop'])->find($itemid);
        if ($item) {
            $item->incrementViews();
            if ($item->on_sale) {
                $item->thumb = image_url($item->thumb);
                $item->image = image_url($item->image);

                $content = $item->content;
                if ($content) {
                    if ($content->content) {
                        $content->content = cleanUpStyle($content->content);
                    } else {
                        $content->content = '<p><img src="'.$item->image.'"></p>';
                    }
                } else {
                    $content->content = '<p><img src="'.$item->image.'"></p>';
                }

                $images = $item->images->map(function ($image){
                    $image->thumb = image_url($image->thumb);
                    $image->image = image_url($image->image);
                    return $image;
                });

                $shop = $item->shop;
                $shop->logo = image_url($shop->logo);
                $shop->item_count = Item::where(['shop_id'=>$item->shop_id])->onSale()->count();

                $properties = $item->props;

                return ajaxReturn(compact('itemid', 'item', 'content', 'images', 'shop', 'properties'));
            } else {
                return ajaxError(1, trans('mall.commodity has already gone off shelves'));
            }
        }else {
            return ajaxError(404, 'item not found');
        }
    }

    /**
     * 批量获取商品信息
     * @return \Illuminate\Http\JsonResponse
     */
    public function getItemList(){
        $offset = $this->request->input('offset');
        $count  = $this->request->input('count');
        $offset = $offset ? $offset : 0;
        $count  = $count ? $count : 20;

        $query = Item::with(['shop'])->onSale();

        $keywords = $this->request->input('keywords');
        if ($keywords) {
            $query = $query->where('title', 'LIKE', "%$keywords%")->orWhereHas('shop', function ($query) use ($keywords){
                return $query->where('shop_name', 'LIKE', "%$keywords%");
            });
        }

        $catid = $this->request->input('catid');
        if ($catid) {
            $catIds = ItemCatlog::fetchAllChildIds($catid);
            $query = $query->whereIn('catid', $catIds);
        };

        $shop_id = $this->request->input('shop_id');
        if ($shop_id) $query = $query->where('shop_id', $shop_id);

        $sort = $this->request->input('sort');
        if ($sort == 'sold-desc') {
            $query = $query->orderByDesc('sold');
        } elseif ($sort == 'rate-desc') {
            $query = $query->orderByDesc('reviews');
        } else {
            $query = $query->orderByDesc('itemid');
        }

        $items = $query->offset($offset)->limit($count)->get();
        return ajaxReturn([
            'total_count'=>$query->count(),
            'offset'=>$offset,
            'count'=>$items->count(),
            'items'=>$items->map(function ($item){
                $item->thumb = image_url($item->thumb);
                $item->image = image_url($item->image);
                if ($item->shop) {
                    $item->shop->logo = image_url($item->shop->logo);
                    $item->shop_name = $item->shop->shop_name;
                    $item->province = $item->shop->province;
                    $item->city = $item->shop->city;
                    $item->district = $item->shop->district;
                } else {
                    $item->shop = [];
                }
                return $item;
            })
        ]);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCatlog(){
        $catid = $this->request->input('catid');
        $catlog = ItemCatlog::find($catid);
        if ($catlog) {
            return ajaxReturn(['catlog'=>$catlog]);
        }else {
            return ajaxError(404, 'catlog not exists');
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCatlogList(){
        $fid = intval($this->request->input('fid'));

        $items = ItemCatlog::where('fid', $fid)->available()->get()->map(function ($catlog){
            $catlog->icon = image_url($catlog->icon);
            return $catlog;
        });
        return ajaxReturn(['items'=>$items]);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCatlogTree(){
        return ajaxReturn(['items'=>ItemCatlog::fetchWithCache()]);
    }
}
